<template>
  <el-card>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
        <el-form-item label="模板组" prop="templateGroupId">
          <el-select v-model="state.queryForm.templateGroupId" style="width: 100%" placeholder="请选择模板信息" @change="getDataList">
            <el-option v-for="p in state.templateGroupList" :key="p.value" :label="p.label" :value="p.value"></el-option>
          </el-select>
        </el-form-item>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateHandle()">上传</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
        </el-form-item>
      </el-form>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%"
              @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
      <el-table-column prop="fileName" label="文件名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="filePath" label="文件路径" header-align="center" align="center"></el-table-column>
      <el-table-column prop="fileType" label="文件类型" header-align="center" align="center"></el-table-column>
      <el-table-column prop="genFileName" label="生成的文件名" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="180">
        <template #default="scope">
          <el-button type="primary" link @click="getFileContentHandle(scope.row.id, scope.row.fileType)">查看</el-button>
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        :current-page="state.pageNum"
        :page-sizes="state.pageSizes"
        :page-size="state.pageSize"
        :total="state.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
    >
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
    <show-file-content ref="showFileContentRef"></show-file-content>
  </el-card>
</template>

<script setup lang="ts">
import {useCrud} from '@/hooks'
import {onMounted, reactive, ref} from 'vue'
import AddOrUpdate from './add-or-update.vue'
import {ITemplateFileOptions} from "@/types/templateFile";
import {useTemplateGroupOptionsApi} from "@/api/templateGroup";
import ShowFileContent from "@/views/template-file/show-file-content.vue";

const state: ITemplateFileOptions = reactive({
  // 不自动调用接口
  createdIsNeed: false,
  dataListUrl: '/generator/v1/templateFile/page',
  deleteUrl: '/generator/v1/templateFile',
  queryForm: {
    // 模板组 ID
    templateGroupId: '',
  },
  // 模板组信息
  templateGroupList: []
})

onMounted(() => {
  // 获取模板组列表
  getTemplateGroupOptionList()
})

/**
 * 获取全部模板组列表
 */
const getTemplateGroupOptionList = async () => {
  useTemplateGroupOptionsApi().then(res => {
    state.templateGroupList = res.data
    // 模板组 ID
    state.queryForm!!.templateGroupId = state.templateGroupList[0].value
    // 获取数据
    getDataList()
  })
}

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(state.queryForm?.templateGroupId, id)
}

const showFileContentRef = ref()
const getFileContentHandle = (id: number, fileType: string) => {
  showFileContentRef.value.init(id, fileType)
}

const {getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle} = useCrud(state)
</script>
